package com.jt.tenant.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;

public class MySeekBar extends SeekBar {
    private PopupWindow mPopupWindow;

    private LayoutInflater mInflater;
    private View mView;
    private int[] mPosition;

    private final int mThumbWidth = 25;
    private TextView mTvProgress;

    public MySeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        mInflater = LayoutInflater.from(context);

        mView = mInflater.inflate(R.layout.popwindow_layout, null);

        JTApplication.ScaleScreenHelper.loadView((ViewGroup) mView);

        mTvProgress = (TextView) mView.findViewById(R.id.tvPop);

        mPopupWindow = new PopupWindow(mView, mView.getWidth(),

                mView.getHeight(), false);

        mPosition = new int[2];
    }

    public void setSeekBarText(String str) {

        mTvProgress.setText(str);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                this.getLocationOnScreen(mPosition);

                Log.e("Position",String.valueOf(mPosition[0])+"=========="+String.valueOf(mPosition[1]));

                mPopupWindow.showAsDropDown(this, (int) event.getX(),
                        0);

//                mPopupWindow.showAsDropDown(this, (int) event.getX(),
//                        mPosition[1] - 200);
                break;
            case MotionEvent.ACTION_UP:
                mPopupWindow.dismiss();

                break;
        }

        return super.onTouchEvent(event);
    }

    private int getViewWidth(View v) {
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        v.measure(w, h);
        return v.getMeasuredWidth();
    }

    private int getViewHeight(View v) {
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        v.measure(w, h);
        return v.getMeasuredHeight();
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        int thumb_x = this.getProgress() * (this.getWidth() - mThumbWidth)
                / this.getMax();
        int middle = mPosition[1]+30;
        super.onDraw(canvas);

        if (mPopupWindow != null) {
            try {
                this.getLocationOnScreen(mPosition);
                mPopupWindow.update(thumb_x + mPosition[0]
                                - getViewWidth(mView) / 2 + mThumbWidth / 2, middle,
                        getViewWidth(mView), getViewHeight(mView));

            } catch (Exception e) {
                // TODO: handle exception
            }
        }

    }

}
